﻿using UnityEngine;
using System.Collections;
using Battle.View;

namespace Battle
{
    public class BattleHeroItem : MonoBehaviour
    {

        public UITexture heroHead;
        public UILabel heroName;
        //public UILabel level;
        public SmoothBar hpBar;

        public GameObject skillEnableEffect1;
        public GameObject skillCastEffect1;
        public GameObject skillEnableEffect2;
        public GameObject skillCastEffect2;
        //public UISprite hpSprite;
        //public GameObject skillCastEffect1;
        //public GameObject skillCastEffect2;
        public GameObject ultimateEffect;
        public GameObject ultimatePressEffect;
        public GameObject ultimateCastEffect;
        public GameObject ultimateDragEffect;

        public GameObject dangerousEffectObj;

        public GameObject deathMask;

        public BattleCharacter owner;
        public BattlePanel battlePanel;
        public string uniqueId;
        private string heroResource = null;
        private bool isOwnerDead = false;
        
        void Start()
        {
        }

        public void Init(BattleCharacter bc,BattlePanel battlePanel)
        {
            this.owner = bc;
            this.battlePanel = battlePanel;
            uniqueId = bc.uniqueId;
            if (bc.heroClass == (int)HeroClass.Main_Actor_Man || bc.heroClass == (int)HeroClass.Main_Actor_Woman)
            {
                name = StaticData.playerData.name;
            }
            else {
                name = bc.heroName;
            }
            heroName.text = name; 
            heroResource = bc.resourceName;
#if !FIX_RESOURCE
            Texture2D t2d = AssetLoad.AssetBundleManager.Instance.GetAssetFromBundle("HeroHeader/" + bc.iconName, bc.iconName, typeof(Texture2D)) as Texture2D;
#else
            Texture2D t2d = AssetLoad.ResourceManager.Instance.GetResource(@"HeroHeader/" + bc.iconName) as Texture2D;
#endif


            heroHead.mainTexture = t2d;
            isOwnerDead = !owner.isAlive;
            if (isOwnerDead)
            {
                OnHeroDead();
            }
            else
            {
                deathMask.SetActive(false);
                GetComponent<BoxCollider>().enabled = true;
                heroHead.color = Color.white;
            }
        }

        public void OnClick()
        {
            if(BattleManager.Instance.battleViewCtrl.curRunningCamp == BattleCamp.Defender)
            {
                return;
            }

            if (StaticData.curBattleData.battleType == BattleType.FIRST) {
                var ctrl = BattleViewCtrl.GetInstance() as BattleViewFirstCtrl;
                if (ctrl.curGuideStep == BattleViewFirstCtrl.FirstBattleGuideStep.UltimateSkillNeedFirstCast)
                {
                    return;
                }
                else if (ctrl.curGuideStep == BattleViewFirstCtrl.FirstBattleGuideStep.NormalSkillFirstCasting ||
                    ctrl.curGuideStep == BattleViewFirstCtrl.FirstBattleGuideStep.NormalSkillNeedFirstCast) {
                    if (!owner.IsFirstSkillCanCast4UI() && owner.IsSecondSkillCanCast4UI()) {
                        return;
                    }
                }
            }

            if ( owner.IsFirstSkillCanCast4UI())
            {
                battlePanel.OnSkillBtnClick(uniqueId, (int)SkillType.Skill1);
            }
            else if (owner.IsSecondSkillCanCast4UI())
            {
                battlePanel.OnSkillBtnClick(uniqueId, (int)SkillType.Skill2);
            }
        }

        private Vector3 onPressBeginPos;
        void OnPress(bool isPress)
        {
            if (BattleManager.Instance.battleViewCtrl.curRunningCamp == BattleCamp.Defender)
            {
                return;
            }
            if (isPress)
            {
                if (owner.IsUltimateSkillCanCast4UI())
                {
                    Vector3 wp = UICamera.currentCamera.ScreenToWorldPoint(Input.mousePosition);
                    Vector3 absVec = gameObject.transform.InverseTransformPoint(wp);
                    onPressBeginPos = absVec;
                    ultimatePressEffect.SetActive(true);
                    ultimateDragEffect.SetActive(true);
                    var p = ultimateDragEffect.transform.localPosition;
                    p.y = absVec.y;
                    ultimateDragEffect.transform.localPosition = p;
                    ultimateCastEffect.SetActive(false);
                }
            }
            else
            {
                if (owner.IsUltimateSkillCanCast4UI())
                {
                    Vector3 wp = UICamera.currentCamera.ScreenToWorldPoint(Input.mousePosition);
                    Vector3 absVec = gameObject.transform.InverseTransformPoint(wp);
                    ultimateDragEffect.SetActive(false);
                    if (absVec.y - onPressBeginPos.y > 100f)
                    {
                        battlePanel.OnSkillBtnClick(uniqueId, (int)(SkillType.Ultimate));
                        AudioManager.Instance.PlayUIAudio("bisha");
                        ultimateCastEffect.SetActive(true);
                        var p = ultimateCastEffect.transform.localPosition;
                        p.y = Mathf.Min(151f, absVec.y);
                        ultimateCastEffect.transform.localPosition = p;
                    }

                }
                if (ultimatePressEffect.activeSelf)
                {
                    ultimatePressEffect.SetActive(false);
                }
            }
        }

        public void ShowUltimateHelpTip() {
            ultimatePressEffect.SetActive(true);
        }

        public void OnDrag(Vector2 pos)
        {
            if(ultimateDragEffect.activeSelf)
            {
                Vector3 wp = UICamera.currentCamera.ScreenToWorldPoint(Input.mousePosition);
                Vector3 absVec = gameObject.transform.InverseTransformPoint(wp);
                var p = ultimateDragEffect.transform.localPosition;
                p.y = Mathf.Min(151f,absVec.y);
                ultimateDragEffect.transform.localPosition = p;
            }
        }



        public void TurnEnter()
        {
            gameObject.SetActive(true);
            if (isOwnerDead)
            {
                deathMask.SetActive(true);
                return;
            }
            heroHead.color = Color.white;
            skillEnableEffect1.SetActive(owner.IsFirstSkillCanCast4UI());
            skillEnableEffect2.SetActive(owner.IsSecondSkillCanCast4UI());
            ultimateEffect.SetActive(owner.IsUltimateSkillCanCast4UI());

            ultimatePressEffect.SetActive(false);
            ultimateCastEffect.SetActive(false);
            ultimateDragEffect.SetActive(false);

            deathMask.SetActive(false);
            GetComponent<BoxCollider>().enabled = true;
        }

        public void TurnOver()
        {
            skillEnableEffect1.SetActive(false);
            skillEnableEffect2.SetActive(false);
            ultimatePressEffect.SetActive(false);
            ultimateCastEffect.SetActive(false);
            ultimateDragEffect.SetActive(false);
            heroHead.color = new Color(0.3f, 0.3f, 0.3f);
            //gameObject.SetActive(false);
            GetComponent<BoxCollider>().enabled = false;
        }

        //角色死亡
        private void OnHeroDead()
        {
            //heroHead.shader = Shader.Find("Unlit/Colorless");
            GetComponent<BoxCollider>().enabled = false;
            heroHead.color = new Color(0.3f, 0.3f, 0.3f);
            hpBar.SetValue(0);
            skillEnableEffect1.SetActive(false);
            skillEnableEffect2.SetActive(false);
            deathMask.SetActive(true);
            dangerousEffectObj.SetActive(false);
        }

        private void OnHeroReborn() {
            TurnEnter();
        }
        

        //释放技能
        public void CastSkill(SkillType type)
        {
            switch (type)
            {
                case SkillType.Skill1:
                    skillEnableEffect1.SetActive(false);
                    skillCastEffect1.SetActive(true);
                    break;
                case SkillType.Skill2:
                    skillEnableEffect2.SetActive(false);
                    skillCastEffect2.SetActive(true);
                    break;
                case SkillType.Ultimate:
                    ultimateEffect.SetActive(false);
                    break;
            }
            if(!HadSkill())
            {
                heroHead.color = new Color(0.3f, 0.3f, 0.3f);
            }
        }

        public bool HadSkill()
        {
            return owner.IsUltimateSkillCanCast4UI() ||
                owner.IsSecondSkillCanCast4UI() || owner.IsFirstSkillCanCast4UI();
        }

        public void OnDestroy()
        {
            heroHead.mainTexture = null;
            if(heroResource != null)
            {
                heroResource = null;
            }
        }

        public void Update()
        {
            if (owner == null)
            {
                return;
            }
            UpdateHpBar();
            if (!isOwnerDead && !owner.isAlive)
            {
                isOwnerDead = true;
                OnHeroDead();
            }

            if (isOwnerDead && owner.isAlive)
            {
                isOwnerDead = false;
                OnHeroReborn();

            }

            if (battlePanel.viewCtrl.curRunningCamp == BattleCamp.Attacker && owner.IsUltimateSkillCanCast4UI())    //大招效果
            {
                if (!ultimateEffect.activeSelf)
                {
                    ultimateEffect.SetActive(true);
                    heroHead.color = Color.white;
                    GetComponent<BoxCollider>().enabled = true;
                    heroHead.color = Color.white;
                }
            }
            else
            {
                if (ultimateEffect.activeSelf)
                {
                    ultimateEffect.SetActive(false);
                    if (!HadSkill())
                    {
                        heroHead.color = new Color(0.3f, 0.3f, 0.3f);
                    }
                    else
                    {
                        heroHead.color = Color.white;
                    }
                }
            }

        }
        //处理血量变化
        private double preHp = 0;
        private double preMaxHp = 0;
        private void UpdateHpBar()
        {
            if (preHp == owner.hp && preMaxHp == owner.maxHp || isOwnerDead)
            {
                return;
            }
            preHp = owner.hp;
            preMaxHp = owner.maxHp;
            if(preHp / preMaxHp < 1.0 / 3.0 && !isOwnerDead)
            {
                dangerousEffectObj.SetActive(true);
            }
            else
            {
                dangerousEffectObj.SetActive(false);
            }
            hpBar.SetValue((int)owner.hp, (int)owner.maxHp);
        }



    }
}